<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\News as NewsModel;

/**
 * Class News 新闻接口
 * @package app\api\controller
 */
class News extends Api
{
    /**
     * 获取新闻列表
     */
    public function get_list()
    {
        $newsModel = model('News');
        $list = [];
        try {
            $type = $this->request->param('type');
            if(!$type){
                $type = 1;
            }
            $where = ['is_release' => NewsModel::RELEASE_YAT, 'type' => $type];
            $list = $newsModel->where($where)->order('weigh desc,id desc')->paginate();
            foreach ($list as &$value) {
                $value['image'] = getRealImg($value['image']);
            }
        } catch (\Exception $e) {
            new \Exception($e->getMessage(), 0);
        }
        $this->success('ok', $list);
    }

    /**
     * 获取新闻详情
     */
    public function get_info()
    {
        $news_id = $this->request->param('id');
        $newsModel = model('News');
        $result = [];
        try {
            $result = $newsModel->where('id', $news_id)->find();
            if(!$result) {
                $this->error('暂无数据', []);
            }
            $result['browse'] += 1;
            $result['image'] = getRealImg($result['image']);
            $info_image = $result['info_image'] ? explode(",",$result['info_image']) : [];
            foreach($info_image as &$item){
                $item = getRealImg($item);
            }
            $result['info_image'] = $info_image;
//            $result['content'] = replace_editor_content_images_url($result['content']);
            model('News')->where(['id'=>$news_id])->setInc('browse');
        } catch (\Exception $e) {
            new \Exception($e->getMessage(), 0);
        }
        $this->success('ok', $result);
    }
    /**
     * 上一篇  下一篇
     */
    public function prevNext(){
        $news_id = $this->request->param('id');
        $type = $this->request->param('type',"");
        $newsModel = model('News');
        $prev = $newsModel->prevData($news_id,$type);
        if(!$prev){
            $prev['id'] = 0;
            $prev['title'] = "没有了";
            $prev['en_title'] = "no more";
        }
        $next = $newsModel->nextData($news_id,$type);
        if(!$next){
            $next['id'] = 0;
            $next['title'] = "没有了";
            $next['en_title'] = "no more";
        }
        $data['prev'] = $prev;
        $data['next'] = $next;
        $this->success('ok', $data);
    }
}